TARGET_NAME = ZINO_AT32F421C8T7_BOOTLOOADER
TARGET_BOARD = ZINO_XIAOGUO
SERIAL_PROT = COM5
MCPU = AT32F421C8T7
BUILD_DATE := $(shell date +"%Y%m%d")
MAIN_VERSION := 1
SUB_VERSION := 0
BUILD_NUMBER := 1
TARGET_VERSION = V$(MAIN_VERSION).$(SUB_VERSION).$(BUILD_NUMBER)
BUILD_VERSION += $(TARGET_VERSION)_$(BUILD_DATE)
TARGET_ELF = ./build/$(TARGET_NAME)_$(BUILD_VERSION).elf
TARGET_BIN = ./build/$(TARGET_NAME)_$(BUILD_VERSION).bin

targets: $(TARGET_BIN) $(TARGET_ELF) $(nOBJS)

linkerScript = ./project/Eclipse_gcc/AT32F421x8_FLASH.ld


lib_inc = \
	./libraries/cmsis/cm4/core_support \
	./libraries/cmsis/cm4/device_support \
	./libraries/cmsis/cm4/device_support/startup \
	./libraries/drivers/inc \
	./project/CRC

libSrcDir = ./libraries/drivers/src
lib_src = \
	./libraries/cmsis/cm4/device_support/system_at32f421.c \
	$(libSrcDir)/at32f421_adc.c \
	$(libSrcDir)/at32f421_cmp.c \
	$(libSrcDir)/at32f421_crc.c \
	$(libSrcDir)/at32f421_crm.c \
	$(libSrcDir)/at32f421_debug.c \
	$(libSrcDir)/at32f421_dma.c \
	$(libSrcDir)/at32f421_ertc.c \
	$(libSrcDir)/at32f421_exint.c \
	$(libSrcDir)/at32f421_flash.c \
	$(libSrcDir)/at32f421_gpio.c \
	$(libSrcDir)/at32f421_i2c.c \
	$(libSrcDir)/at32f421_misc.c \
	$(libSrcDir)/at32f421_pwc.c \
	$(libSrcDir)/at32f421_scfg.c \
	$(libSrcDir)/at32f421_spi.c \
	$(libSrcDir)/at32f421_tmr.c \
	$(libSrcDir)/at32f421_usart.c \
	$(libSrcDir)/at32f421_wdt.c \
	$(libSrcDir)/at32f421_wwdt.c
INC = $(lib_inc) ./project/inc
srcDir = ./project/src
src_c= \
	$(lib_src) \
	$(srcDir)/at32f421_clock.c \
	$(srcDir)/at32f421_int.c \
	$(srcDir)/main.c \
	$(srcDir)/bootloader.c \
	./project/CRC/crc.c \

INCLUDES = $(addprefix -I", $(addsuffix ", $(INC)))
DEPS = $(patsubst ./%,./build/%,$(patsubst %.c, %.d, $(src_c)))
OBJS = $(patsubst ./%,./build/%,$(patsubst %.c, %.o, $(src_c)))
SRC += $(src_c)
SRC += ./libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f421.s
DEPS += ./build/libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f421.d
OBJS += ./build/libraries/cmsis/cm4/device_support/startup/gcc/startup_at32f421.o
#-g -gdwarf-2
c_flags = \
	-mcpu=cortex-m4 -mthumb -mfloat-abi=soft \
	-Os -ffunction-sections -fdata-sections \
	-Wall  -DSOC_FAMILY_AT32 \
	-D$(MCPU) \
	-lm -DARM_MATH_CM4 \
	$(INCLUDES) \
	-std=gnu11 -MMD -MP -MD -MF "$(@:%.o=%.d)" -MT"$(@)"
DATE_TIME = $(shell date +"%Y-%m-%d-%H:%M:%S")
asm_flags = \
	-mcpu=cortex-m4 -mthumb -mfloat-abi=soft \
	-Os -ffunction-sections -fdata-sections -Wall -x assembler-with-cpp \
	-I"./" \
	-Xassembler -mimplicit-it=thumb -MMD -MP

-include $(DEPS)
./build/%.o: %.c
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $< -c -o $@ $(c_flags)
./build/%.o: %.s
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
./build/%.o: %.S
	@ echo "$(DATE_TIME) $< ..."
	@ mkdir -p $(dir $@)
	@ arm-none-eabi-gcc $(asm_flags) -MF"$(@:%.o=%.d)" -MT"$(@)" -c -o "$@" "$<"
m_ldflags= \
	-mcpu=cortex-m4 \
	-mthumb -mfloat-abi=soft -Os \
	-Wall \
	-T "$(linkerScript)" \
	-Wl,-Map,"./build/$(TARGET_NAME).map" \
	-Wl,--no-warn-rwx-segments \
	-Xlinker --gc-sections \
	-Xlinker --cref -Wl,--print-memory-usage
$(TARGET_ELF): $(OBJS)
	@ echo "$(DATE_TIME) Linking everyting together... $@"
	@ arm-none-eabi-gcc -o $(TARGET_ELF) -Wl,--whole-archive $(OBJS) -Wl,--no-whole-archive $(m_ldflags) -lc -lm
$(TARGET_BIN): $(TARGET_ELF)
	@ echo "$(DATE_TIME) Create binary file... $@"
	@ arm-none-eabi-objcopy -O binary $(TARGET_ELF) $(TARGET_BIN)
cl:
	-$(RM) $(OBJS)$(TARGET_ELF)$(DEPS)
	-@echo ' '
dl: $(TARGET_BIN) $(TARGET_ELF)
	@ echo "$(DATE_TIME) Downloading $(TARGET_BIN) to board..."
	chcp 65001
	@ ./tool/at32_isp_loader_v3_win.exe \
	--comport $(SERIAL_PROT) 115200 --isp --getCommands \
	--getVersion \
	--erase bank1 \
	--writefile $(TARGET_BIN) 0x08000000 \
	--resetDevice
#  --getCommands                    Get device command list
#  --getVersion                     Get bootloader version
#  --getDeviceID                    Get device ID
#  --readMemory                     Read memory at a given address
#  --jump                           Jump to a given memory address
#  --writeMemory                    Write to memory at a given address
#  --erase                          Erase a given memory area or the entire chip or blank
#  --eraseAndProgramProtect         Erase/program protection enable
#  --eraseAndProgramUnprotect       Erase/program protection disable
#  --accessProtect                  Access protection enable
#  --accessUnprotect                Access protection disable
#  --firmwareCRC                    Calculate sector CRC
#  --enablesLib                     SLib enable
#  --disablesLib                    SLib disable
#  --getsLibStatus                  Get SLib status
#  --resetDevice                    Reset device
#  --advancedAccessProtect          Advanced access protection enable
#--getVersion \
	--readmemory 0x1FFFF7E0 1 \
	--getDeviceId --getDeviceId \
	--getVersion --getVersion --getVersion \
	--readmemory 0x1FFFF7E0 1 \
	--readmemory 0x1FFFF800 128 \
	--readmemory 0x1FFFF880 128 \
	--readmemory 0x1FFFF900 128 \
	--readmemory 0x1FFFF980 128
